package com.tsing.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.tsing.common.result.PageResult;
import com.tsing.mapper.ArticleMapper;
import com.tsing.mapper.NewsMapper;
import com.tsing.mapper.WmUserMapper;
import com.tsing.mapper.impl.ArticleMapperImpl;
import com.tsing.mapper.impl.NewsMapperImpl;
import com.tsing.mapper.impl.WmUserMapperImpl;
import com.tsing.pojo.dto.NewsDTO;
import com.tsing.pojo.entity.TlPost;
import com.tsing.pojo.entity.TlUser;
import com.tsing.pojo.vo.PostDetailsVO;
import com.tsing.service.NewsService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 吴阳基
 * @datetime 2023/12/4 20:51
 */
public class NewsServiceImpl implements NewsService {
    private final NewsMapper newsMapper = new NewsMapperImpl();
    private final WmUserMapper wmUserMapper = new WmUserMapperImpl();
    private final ArticleMapper articleMapper = new ArticleMapperImpl();

    @Override
    public PageResult<PostDetailsVO> list(Integer page, Integer size, Integer status, String title) {
        //获取用户信息
        List<TlUser> tlUserList = wmUserMapper.list();
        Map<Long, TlUser> tlUserMap = tlUserList.stream().collect(Collectors.toMap(TlUser::getUserId, tlUser -> tlUser));

        //获取用户文章详情表，没有包含用户姓名
        PageResult<TlPost> tlPostPageResult = newsMapper.list(page, size, status, title);

        List<TlPost> tlPostList = tlPostPageResult.getData();

        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        mapper.registerModule(new JavaTimeModule());

        List<PostDetailsVO> postDetailsVOS = tlPostList.stream().map(tlPost -> {
            PostDetailsVO postDetailsVO = mapper.convertValue(tlPost, PostDetailsVO.class);
            TlUser tlUser = tlUserMap.get(tlPost.getUserId());
            if (tlUser != null) {
                postDetailsVO.setUserName(tlUser.getUsername());
            }
            return postDetailsVO;
        }).collect(Collectors.toList());

        // 创建新的PageResult并填充数据
        PageResult<PostDetailsVO> resultVo = new PageResult<>();
        resultVo.setData(postDetailsVOS);
        resultVo.setTotal(tlPostPageResult.getTotal());
        resultVo.setSize(tlPostPageResult.getSize());
        resultVo.setCode(tlPostPageResult.getCode());
        resultVo.setCurrentPage(tlPostPageResult.getCurrentPage());

        return resultVo;
    }

    @Override
    public int fail(Integer id, Integer status, String msg) {
        return newsMapper.update(id, status,msg);
    }

    @Override
    public int pass(Integer id, Integer status) {
        return newsMapper.update(id, status ,null);
    }

    @Override
    public PageResult<PostDetailsVO> findPublished(NewsDTO newsDTO) {
        //获取用户信息
        List<TlUser> tlUserList = wmUserMapper.list();
        Map<Long, TlUser> tlUserMap = tlUserList.stream().collect(Collectors.toMap(TlUser::getUserId, tlUser -> tlUser));

        //获取用户文章详情表，没有包含用户姓名
        PageResult<TlPost> tlPostPageResult = newsMapper.findPublished(newsDTO);

        List<TlPost> tlPostList = tlPostPageResult.getData();

        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        mapper.registerModule(new JavaTimeModule());

        List<PostDetailsVO> postDetailsVOS = tlPostList.stream().map(tlPost -> {
            PostDetailsVO postDetailsVO = mapper.convertValue(tlPost, PostDetailsVO.class);
            TlUser tlUser = tlUserMap.get(tlPost.getUserId());
            if (tlUser != null) {
                postDetailsVO.setUserName(tlUser.getUsername());
            }
            return postDetailsVO;
        }).collect(Collectors.toList());

        // 创建新的PageResult并填充数据
        PageResult<PostDetailsVO> resultVo = new PageResult<>();
        resultVo.setData(postDetailsVOS);
        resultVo.setTotal(tlPostPageResult.getTotal());
        resultVo.setSize(tlPostPageResult.getSize());
        resultVo.setCode(tlPostPageResult.getCode());
        resultVo.setCurrentPage(tlPostPageResult.getCurrentPage());

        return resultVo;
    }

    @Override
    public int down_or_up(Integer id, Integer enable) {
        return newsMapper.down_or_up(id, enable);
    }

    @Override
    public PostDetailsVO one(Integer id) {
        TlPost tlPost = newsMapper.one(id);

        TlUser tlUser = articleMapper.getUserById(Integer.parseInt(tlPost.getUserId().toString()));

        PostDetailsVO postDetailsVO = new PostDetailsVO();

        postDetailsVO.setPostId(tlPost.getPostId());
        postDetailsVO.setUserId(tlPost.getUserId());
        postDetailsVO.setDetailsId(tlPost.getDetailsId());
        postDetailsVO.setTitle(tlPost.getTitle());
        postDetailsVO.setImage(tlPost.getImage());
        postDetailsVO.setLikeCount(tlPost.getLikeCount());
        postDetailsVO.setChannelId(tlPost.getChannelId());
        postDetailsVO.setCreateDate(tlPost.getCreateDate());
        postDetailsVO.setAddress(tlPost.getAddress());
        postDetailsVO.setCopywriting(tlPost.getCopywriting());
        postDetailsVO.setStatus(tlPost.getStatus());
        postDetailsVO.setUserName(tlUser.getUsername());

        return postDetailsVO;
    }
}
